<template>
  <view class="goods-item">
    <!-- 商品左侧图片区域 -->
    <view class="goods-item-left">
      <slot name="radio"></slot>
      <image
        :src="goods.goods_small_logo || defaultPic"
        mode="widthFix"
        class="goods-pic"
      ></image>
    </view>
    <!-- 商品右侧信息区域 -->
    <view class="goods-item-right">
      <!-- 商品标题 -->
      <view class="goods-name">{{ goods.goods_name }}</view>
      <view class="goods-info-box">
        <!-- 商品价格 -->
        <view class="goods-price">￥{{ price }}</view>
        <uni-number-box
          :min="1"
          :value="goods.goods_count"
          @change="numberChangeHandler"
          v-if="showNum"
        ></uni-number-box>
      </view>
    </view>
  </view>
</template>
<script>
import { mapMutations } from 'vuex';
export default {
  data() {
    return {
      // 默认的空图片,防止有的图片没有数据
      defaultPic:
        'https://img0.baidu.com/it/u=3658834790,1651132635&fm=253&fmt=auto&app=138&f=JPEG?w=642&h=500'
    };
  },
  props: {
    // 接收父组件传过来的item项
    goods: {
      type: Object,
      default: ''
    },
    goodsList: {
      type: Array,
      default: []
    },
    // 是否展示价格右侧的 NumberBox 组件
    showNum: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    // 商品价格展示小数后面两位
    price() {
      return Number(this.goods.goods_price).toFixed(2);
    }
  },
  methods: {
    ...mapMutations('moduleCart', ['updateGoodsState']),
    // 给父组件通过自定义事件把id和数量传回去
    numberChangeHandler(val) {
      this.$emit('num-change', {
        // 商品id
        goods_id: this.goods.goods_id,
        // 商品数量
        goods_count: +val
      });
    }
  },
  onLoad() {
    this.$emit('change-Radio', this.updateGoodsState(goods.goods_id));
  }
};
</script>
<style lang="scss">
.goods-item {
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;
  .goods-item-left {
    margin-right: 5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
    }
    radio {
    }
  }
}

.goods-item-right {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: space-between;
  .goods-name {
    font-size: 13px;
  }
  .goods-info-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .goods-price {
      color: #c00000;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }
  }
}
</style>
